#coding=UTF-8 
from chinacQA.Static import testEnvtConf
from chinacQA.TestCase import TestCase
from chinacQA.TestLink_TestCase import assertBool
from chinacQA.TestLink_TestCase import getConfigure_ENG
from chinacQA.TestLink_TestCase import setConfigure_ENG

from chinacQA.TestLink_TestCase import waitMoment
from template.NeoCUUI_ENG import *
from TC_100_NeoCU_Language import STEP_AdminLoginAndChangeLanguage


import random

def generateRom():
    seed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    sa = []
    for i in range(8):
        sa.append(random.choice(seed))
    salt = ''.join(sa)
    return salt

def Login(user, pw):
    idusername.text=user
    idpassword.text=pw
    loginBtn.click()

def STEP_AddQos():
    ResourceManageTab.click()
    NetworkMenu_tab.click()
    QosMenu.click()
    qoses_action_create.click()
    QosChooseProject.click()    
    menusub = QosChooseProject.element.find_elements("xpath",".//li")
    for buttonmsub in menusub:
        if (getConfigure_ENG("ProjectName") in buttonmsub.text.strip()):
            buttonmsub.click()
            break       
    Qosidname.text = getConfigure_ENG("QosName_01")
    Qosiddescription.text = getConfigure_ENG("QosDec_01")
    Qosidmax_rate.text = getConfigure_ENG("QosRate_01")
    QosCreateButton.click()
    waitMoment(5)
    QosMenu.click()
    status =False
    heads = qosesTable.getHeads()
    columnName = heads.index('Name')
    columnPro = heads.index('Project')
    for i in range(len(qosesTable.getRows())):
        if(qosesTable.getCell(i,columnName).text.strip()==getConfigure_ENG("QosName_01") and qosesTable.getCell(i,columnPro).text.strip()==getConfigure_ENG("ProjectName") ):
            status =True
            break
        
    assertBool(status,"3447")

def STEP_EditQos():
    QosMenu.click()
    waitMoment(3)
    heads = qosesTable.getHeads()
    columnName = heads.index('Name')
    columnPro = heads.index('Project')
    columnAction = heads.index('Actions')
    for i in range(len(qosesTable.getRows())):
        if(qosesTable.getCell(i,columnName).text.strip()==getConfigure_ENG("QosName_01") and qosesTable.getCell(i,columnPro).text.strip()==getConfigure_ENG("ProjectName") ):
            actioncell=qosesTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="Edit"):
                    button.click()
                    break
            break
    
    QosEditidname.text = getConfigure_ENG("QosNewName")
    QosEditdescription.text = getConfigure_ENG("QosNewDec")
    QosEditidmax_rate.text = getConfigure_ENG("QosNewRate")
    QosEditCommit.click()
    
    waitMoment(3)
    for i in range(len(qosesTable.getRows())):
        if(qosesTable.getCell(i,columnName).text.strip()==getConfigure_ENG("QosNewName") and qosesTable.getCell(i,columnPro).text.strip()==getConfigure_ENG("ProjectName") ):
            status =True
            break
        
    assertBool(status,"3449")
    
def registerNewUser():
    BusinessManager.click()
    ManagerInfo.click()
    tenants_action_create.click()
    projectN = getConfigure_ENG("ProjectName")+generateRom()
    setConfigure_ENG("ProjectName",projectN)
    
    idname.text = projectN
    iddescription.text = getConfigure_ENG("ProjectDec")
    tabQuota.click()
    rows= quotaTable.getRows()
    for row in rows[1:]:
        lines = row.find_elements("xpath",".//td")
        cell = lines[0].find_element("xpath",".//input")
        if(cell.is_selected()):
            pass
        else:
            lines[0].find_element("xpath",".//label").click()
        #lines[0].find_element("xpath",".//label").click()    
    createButton.click()
    waitMoment(3)
    UserManager.click()
    users_action_create.click()
    userName = getConfigure_ENG("User")+generateRom()
    setConfigure_ENG("User",userName)
    idname_u.text =userName
    iduser_name_u.text = getConfigure_ENG("UserName")
    idemail.text = getConfigure_ENG("Useremail")
    idmobile.text = getConfigure_ENG("Usermobile")
    idpassword.text = getConfigure_ENG("UserOldPassword")
    idconfirm_password.text = getConfigure_ENG("UserOldPassword")
    projectButton.click()
    cellPros = selectProject.element.find_elements("xpath",".//li")
    for cellPro in cellPros:
        if (cellPro.text.strip()==getConfigure_ENG("ProjectName")):
            cellPro.click()

    proje.click()
    cellroles = selectidrole.element.find_elements("xpath",".//li")
    for cellPro in cellroles:
        if (cellPro.find_element("xpath",".//a").text.strip()=="project_admin"):
            cellPro.click()
            
    createUserButton.click()
    
def ModifdyPW():
    currentPW.text = getConfigure_ENG("UserOldPassword")
    NewPW.text = getConfigure_ENG("UserNewPassword")
    ConfirmPW.text = getConfigure_ENG("UserNewPassword")
    confirmButton.click()
    waitMoment(6)

def logonOut():
    adminrole.click()
    waitMoment(6)
    loginout.click()

def deleteProAndUser():
    BusinessManager.click()
    ManagerInfo.click()
    tenants_project_filter__q.text = getConfigure_ENG("ProjectName")
    tenants_action_project_filter.click()
    heads = tenants.getHeads()
    columnName = heads.index('Name')
    columnAction = heads.index('Actions')
    for i in range(len(tenants.getRows())):
        if(tenants.getCell(i,columnName)!=None and tenants.getCell(i,columnName).text.strip()==getConfigure_ENG("ProjectName")):
            actioncell=tenants.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//ul/li")
            for button in menubuttons:
                if (button.text.strip()=="Delete"):
                    button.click()
                    deletePro.click()
                    break
                
    status = True  
    if("No items to display." in tenants.getCell(0,0).text.strip()):
        pass    
    else:
        for i in range(len(tenants.getRows())):
            if(tenants.getCell(i,columnName)!=None and tenants.getCell(i,columnName).text.strip()==getConfigure_ENG("ProjectName")):
                status =False
                break
    #assertBool(status,"3175")   
             
    UserManager.click()
    users_user_filter__q.text = getConfigure_ENG("User")
    users_action_user_filter.click()
    heads = userstable.getHeads()
    columnName = heads.index('Login Name')
    columnAction = heads.index('Actions')
    for i in range(len(userstable.getRows())):
        if(userstable.getCell(i,columnName)!=None and userstable.getCell(i,columnName).text.strip()==getConfigure_ENG("User")):
            actioncell=userstable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="Delete"):
                    button.click()
                    deleteuser.click()
                    break
    
    userStatus = True
    if("No items to display." in userstable.getCell(0,0).text.strip()):
        pass
    else:
        for i in range(len(userstable.getRows())):
            if(userstable.getCell(i,columnName).text.strip()==getConfigure_ENG("User")):
                userStatus = False
                break
            
def STEP_AdminLogin():
    Login(testEnvtConf.AdminName, testEnvtConf.AdminPassword)

def STEP_RegisterUserAndPro():
    deleteProAndUser()
    registerNewUser()
    
def STEP_UserLogin():
    Login(getConfigure_ENG("User"),getConfigure_ENG("UserOldPassword"))
    ModifdyPW()
    
def STEP_ChangeLanguage():
    adminrole.click()
    Language.click()

TC_101_NeoCUInitial = TestCase("Neocu Eng Initialize")
TC_101_NeoCUInitial.AddStep(STEP_AdminLoginAndChangeLanguage)
TC_101_NeoCUInitial.AddStep(STEP_AdminLogin)
TC_101_NeoCUInitial.AddStep(STEP_RegisterUserAndPro)
TC_101_NeoCUInitial.AddStep(STEP_AddQos)
TC_101_NeoCUInitial.AddStep(STEP_EditQos)
TC_101_NeoCUInitial.AddStep(logonOut)
TC_101_NeoCUInitial.AddStep(STEP_UserLogin)

if __name__ == "__main__":
    TC_101_NeoCUInitial.run()
